package com.atguigu.msmservice.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.MsmUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@Service
public class MsmServiceImpl implements MsmService {

    //短信验证码的发送
    @Override
    public boolean send(Map<String, Object> map,String phone) {

        if(StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", MsmUtils.ACCESS_KEY_ID, MsmUtils.ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        //设置参数
//        CommonRequest request = new CommonRequest();
//        request.setSysMethod(MethodType.POST);
//        request.setSysDomain("dysmsapi.aliyuncs.com");
//        request.setSysVersion("2017-05-25");
//        request.setSysAction("SendSms");

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //发送设置
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName",MsmUtils.SIGN_NAME);//签名名称
        request.putQueryParameter("TemplateCode", MsmUtils.SMS_CODE);//模板code
        request.putQueryParameter("TemplateParam",JSONObject.toJSONString(map));//验证码,转成

        //发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
//            getData(client,phone);//用于返回发送信息的状态
            return success;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }


    public void getData(IAcsClient client,String phone){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(date);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("QuerySendDetails");
//        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumber", phone);
        request.putQueryParameter("SendDate", format);
        request.putQueryParameter("PageSize", "10");//分页
        request.putQueryParameter("CurrentPage", "1");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
